package cn.kgc.vue.controller;

import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Course;
import cn.kgc.vue.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author 课工场
 * @date 2023/12/14
 * @description
 */
@Api(tags = "课程模块")
@RestController
@RequestMapping("courses")
public class CourseController {

    @Resource
    private CourseService courseService;

    @GetMapping("select")
    @ApiOperation("课程下拉选")
    public ResponseResult getCourseSelect(){
        List<Course> list = courseService.list();
        return  ResponseResult.success().data(list);
    }

    @GetMapping("getById/{cid}")
    @ApiOperation("根据id获取课程信息")
    public ResponseResult getCourseById(@PathVariable Integer cid){
        Course course = courseService.getById(cid);
        return  ResponseResult.success().data(course);
    }


    @DeleteMapping("{cid}")
    @ApiOperation("根据id获取课程信息")
    public ResponseResult removeCourseById(@PathVariable Integer cid){

        // 逻辑删除
        courseService.removeById(cid);

        return  ResponseResult.success();
    }


    @PostMapping("list")
    @ApiOperation("课程列表")
    public ResponseResult getCourseList(@RequestBody QueryCondition<Course> queryCondition){

        Page<Course> coursePage = new Page<>(queryCondition.getPage(), queryCondition.getLimit());

        LambdaQueryWrapper<Course> lambda = new QueryWrapper<Course>().lambda();
        lambda.eq(StringUtils.isNotEmpty(queryCondition.getQuery().getCourseName()),Course::getCourseName,queryCondition.getQuery().getCourseName());

        courseService.page(coursePage,lambda);

        HashMap<String, Object> result = new HashMap<>();
        result.put("pageData",coursePage.getRecords());
        result.put("total",coursePage.getTotal());
        return  ResponseResult.success().data(result);
    }

    @PostMapping("saveOrUpdate")
    @ApiOperation("添加编辑课程")
    public ResponseResult saveOrUpdateCourse(@RequestBody Course course){

        LambdaQueryWrapper<Course> lambda = new QueryWrapper<Course>().lambda();
        lambda.eq(Course::getCourseName,course.getCourseName())
                .or()
                .eq(Course::getCourseNo,course.getCourseNo());

        Course one = courseService.getOne(lambda);
        if (ObjectUtils.isNotNull(one)){
            return ResponseResult.fail().message("课程名或课程编号已存在");
        }

        courseService.saveOrUpdate(course);

        return  ResponseResult.success();
    }


    @GetMapping("screenData")
    @ApiOperation("数据可视化接口")
    public ResponseResult screenData(){

        return  courseService.screenData();
    }

}
